import React, {Component,Fragment} from 'react';
import PropTypes from 'prop-types';
import { connect } from 'dva';
import Exception403 from '../pages/Exception/Exception403';
import { filterAuth } from '@/support/common';
import CommonContext from './CommonContext';


const mapStateToProps = state => ({
  menu: state.menu,
});
class AuthSimpleLayout extends Component{
  static childContextTypes = {
    permissions: PropTypes.array,
    dicData: PropTypes.array,
  };
  getChildContext() {
    const { menu: { permissions, dicData }} = this.props;
    return { permissions, dicData };
  }
  render() {
    const { menu: { permissions,dicData },location:{ pathname = '/' } } = this.props;
    // 存在权限数据 => 判断    不存在权限数据（页面刷新）=》默认就是有权限
    const isAuth = permissions.length > 0 ? filterAuth(pathname,permissions) : true;
    return (
        <Fragment>
          <CommonContext.Provider value={{ permissions,dicData }}>
            {
              pathname === '/' || pathname === '/user/accountSetting' || pathname.includes("Exception") || isAuth ? ({...this.props.children}) : <Exception403/>
            }
          </CommonContext.Provider>
        </Fragment>
    )
  }
}
export default connect(mapStateToProps)(AuthSimpleLayout);


/**
 *   同时实现了新版与旧版的context方法，兼容原有逻辑。
 *   新版使用方法位于CommonContext文件中
 */
